<import file="block/common.html" name="url" :tpl_cfg="['urlName'=>$title,'backList'=>'返回列表','cid'=>$cid??'']"/>
<div class="layui-fluid">
	<div class="layui-form">
		<div class="layui-form-item">
			<label class="layui-form-label required">选择栏目</label>
			<div class="layui-input-inline">
				<select name="cid" lay-verify="">
					<php foreach="$list as $key=>$vo">
					  <option value="<{$vo['id']}>" <?php echo $cid == $vo['id']?'selected':''; echo $vo['type']==0 ?'disabled':'';?>><{$vo['cname']}></option>
					</php> 
					</select> 
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">标题</label>
			<div class="layui-input-block">
				<input type="text" name="title" lay-verify="required" lay-reqtext="标题不能为空" placeholder="请输入标题"
					value="<{$info['title']??''}>" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item layui-form-text">
			<label class="layui-form-label">内容</label>
			<div class="layui-input-block">
				<textarea id="contentEdit" name="content"><{$info['content']??''}></textarea>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">审核人</label>
			<div class="layui-input-inline">
				<input type="text" name="examine" lay-verify="required" lay-reqtext="审核人不能为空" placeholder="请输入审核人"
					value="<{$info['examine']??''}>" class="layui-input">
			</div>
		</div>
		<?php if(SysRolCk("content_attribute")){ ?>
		<div class="layui-form-item">
			<label class="layui-form-label required">内容属性</label>
			<div class="layui-input-block">
				<input type="checkbox" name="attribute_a" value="1" title="头条"
					<?php if(!empty($info)){echo $info['attribute_a'] == 1?'checked=""':'';}?>>
				<input type="checkbox" name="attribute_b" value="1" title="小头条"
					<?php if(!empty($info)){echo $info['attribute_b'] == 1?'checked=""':'';}?>>
				<span id="lunhuantu">
					<input type="checkbox" name="attribute_c" value="1" title="图片轮换"
					<?php if(!empty($info)){echo $info['attribute_c'] == 1?'checked=""':'';}?>>
				</span>
			</div>
		</div>
		<?php } ?>
		<?php if(SysRolCk("upload_upload")){ ?>
		<div class="layui-form-item">
			<label class="layui-form-label required">轮换缩略图</label>
			<div class="layui-input-block">
				<input type="text" id="imageval" name="img" value="<{$info['img']??''}>" class="layui-input">
				<!-- <button type="button" class="layui-btn" id="thumimg">
				  <i class="layui-icon">&#xe67c;</i>上传图片
				</button> -->
				<button type="button" class="layui-btn" id="getimage" onclick="getFirstImg()">
					<i class="layui-icon">&#xe60d;</i>获取内容第一张
				</button>
			</div>
		</div>
		<?php } ?>
		<?php if(SysRolCk("upload_upload") && SysRolCk("attment_index")){ ?>
		<div class="layui-form-item">
			<label class="layui-form-label required">附件</label>
			<div class="layui-input-block">
				<div id="attval" class="cxuu_attmentupload">
				<?php
				if(!empty($info)){
					foreach($infoatt as $att){
						echo '<span id="attidd'.$att['attid'].'" class="layui-badge layui-bg-blue">';
						echo 'ID：'.$att['attid'].' 文件名：'.$att['priname'].'';
						echo '<a href="javascript:;" onclick="attColse('.$att['attid'].')" ><i class="layui-icon layui-icon-close"></i></a>';
						echo '</span>';
					}
				}
				?>
				</div>
				<input type="hidden" name="attid" value="<{$info['attid']??''}>" id="attid">
				<button type="button" class="layui-btn" id="attupload">
					<i class="layui-icon">&#xe67c;</i>上传附件
				</button>
				<tip>可多个附件连续上传，单个附件不超过<{$upload['attuploadsize']}>KB！ 支持格式：<{$upload['attuploadext']}></tip>
			</div>
		</div>
		<?php } ?>
		<div class="layui-form-item">
			<label class="layui-form-label">状态</label>
			<div class="layui-input-inline">
				<?php if(SysRolCk("content_status")){ ?>
					<input type="radio" name="status" value="1" title="发布" <?php if(!empty($info)){if($info['status'] == 1){echo 'checked';}}?>>
				<?php } ?>
					<input type="radio" name="status" value="0" title="草稿" <?php if(!empty($info)){if($info['status'] == 0){echo 'checked';}}?>>
			</div>
				<?php if(!SysRolCk("content_status")){ ?>
					<div class="layui-form-mid layui-word-aux">你没有直接发布内容权限，如果你修改了本条内容，所有该本条内容的头条、小头条或其它不属于您权限的设置将失效！</div>
				<?php } ?>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<input type="hidden" name="id" value="<{$info['id']??''}>">
				<button class="layui-btn" id="submitPost" lay-submit lay-filter="saveBtn"><i class="layui-icon">&#x1005;</i><?php echo lang('save');?></button>
			</div>
			
		</div>
	</div>
</div>
<script type="text/javascript" src="<?php echo U_RES;?>/lib/tinymce/tinymce.min.js?v=5.10.7"></script>
<script>
	layui.define(function () {
		var form = layui.form,
			layer = layui.layer,
			table = layui.table,
			upload = layui.upload,
			$ = layui.$;

		tinymce.init({
			selector: '#contentEdit',
			language: 'zh_CN',
			relative_urls: false,
			remove_script_host: true,
			plugins: 'preview searchreplace autolink directionality visualblocks visualchars image link media template code codesample table charmap pagebreak nonbreaking anchor insertdatetime advlist lists wordcount help emoticons autoresize axupimgs',
			toolbar: 'code | undo redo restoredraft | cut copy paste | forecolor bold italic underline link | alignleft aligncenter alignright alignjustify outdent indent | fontselect fontsizeselect | table <?php if(SysRolCk("upload_upload")){ ?>image<?php } ?> charmap emoticons insertdatetime preview | <?php if(SysRolCk("upload_upload")){ ?>axupimgs<?php } ?>',
			//引用lineheight插件，会导致  Uncaught SyntaxError: Unexpected token '<'  错误，暂时未解决！！
			autosave_interval: "20s",//自动存稿时间
			font_formats: '微软雅黑=Microsoft YaHei,sans-serif;仿宋_GB2312=仿宋_GB2312,Microsoft YaHei,sans-serif;宋体=宋体;仿宋=仿宋;黑体=黑体',
			height: 650, //编辑器高度
			min_height: 400,
			lineheight_formats : '1 1.5 2 2.5 3',
			draggable_modal: true,
			images_upload_base_path: '/',
			images_upload_handler: function (blobInfo, succFun, failFun) {
			var xhr, formData;
			var file = blobInfo.blob();//转化为易于理解的file对象
			xhr = new XMLHttpRequest();
			xhr.withCredentials = false;
			xhr.open('POST', '<{CAUrl("upload","upload")}>&up=img&tinymce');
			xhr.onload = function() {
				var json;
				if (xhr.status != 200) {
					failFun('HTTP Error: ' + xhr.status);
					return;
				}
				json = JSON.parse(xhr.responseText);
				if (!json || typeof json.location != 'string') {
					failFun('Invalid JSON: ' + xhr.responseText);
					return;
				}
				succFun(json.location);
			};
			formData = new FormData();
			formData.append('file', file, file.name );
			xhr.send(formData);
		}
		});

		form.render();

		//监听提交 +=>
		form.on('submit(saveBtn)', function (data) {
			data.field.content = tinyMCE.activeEditor.getContent();
			$.post('<{CAUrl("content",ROUTE["act"])}>',data.field,function (res) {
					if (res.status == 1) {
						layer.msg(res.info, { icon: 1, time: 1000 }, function () {
							loadMain('<{CAUrl("content","index")}>&cid=' + res.cid);
						});
					} else {
						layer.msg(res.info, { icon: 2, anim: 6, time: 5000 });
					}
				});
			//$('#submitPost').addClass('layui-btn-disabled');
			return false;
		});


		var uploadInst = upload.render({
			elem: '#thumimg' //绑定元素
			, url: '<{CAUrl("upload","upload")}>&up=img' //图片上传接口
			, size: "<{$upload['imguploadsize']}>"
			, accept: 'images'
			, acceptMime: 'image/*'		
			, exts: "<{$upload['imguploadext']}>".replace(/./g, '')
			, done: function (res) {
				if (res.status == 1) {
					$('#imageval').val(res.result[0].src);
					layer.msg(res.info);
				} else {
					layer.msg(res.info);
				}
			}
			, error: function () {
				//请求异常回调
			}
		});
		var uploadAttment = upload.render({
			elem: '#attupload' //绑定元素
			, url: '<{CAUrl("upload","upload")}>&up=att' //附件上传接口
			, size: "<{$upload['attuploadsize']}>"
			, accept: 'file'
			, acceptMime: 'file/*'
			//, exts: 'zip|rar|7z|doc|docx|xlsx|xls|'
			, exts: "<{$upload['attuploadext']}>".replace(/./g, '')
			, done: function (res) {
				if (res.status == 1) {
					$('#attval').append('<span id="attidd'+ res.prekey +'" class="layui-badge layui-bg-blue"> ID：'+res.prekey+' 文件名：'+ res.result[0].originName + ' <a href="javascript:;" onclick="attColse('+res.prekey+')" ><i class="layui-icon layui-icon-close"></i></a></span>');
					var input = $("#attid").val();
					if(input != "" && input != undefined){
						$("#attid").val($("#attid").val()+","+res.prekey);
					}else{
						$('#attid').val(res.prekey);
					}
					//res.result[0].src + res.result[0].suffix + res.result[0].size
					layer.msg(res.info);
				} else {
					layer.msg(res.info[0]);
				}
			}
			, error: function () {
				//请求异常回调
			}
		});

	});
	//删除已经上传附件
	function attColse(id){
		layer.confirm('真的删除这个附件吗？',{icon: 3, title:'警告'},  function (index) {
			$.get('<{CAUrl("upload","upload")}>&up=att&del', { attid: id},function(data){
				if(data.status==1){
					layer.msg("附件: " + data.info);
					var input = $("#attid").val();
					var newarr = input.split(",");
					newarr.splice($.inArray(""+id+"",newarr),1);
					var newstring = newarr.join(",");
					$('#attid').val(newstring);
					$('#attidd'+id).remove();
				}else{
					layer.msg("附件: " + data.info+"删除失败！");
				}
			});
		layer.close(index);
		});
	}

	//获取编辑器内容的第一张图片
	function getFirstImg() {
		var content = tinyMCE.activeEditor.getContent();
		var getval = $(content).find("img").eq(0).attr("src");
		if (getval.length > 0) {
			$('#imageval').val(getval);
			$('#lunhuantu').show(); //如果有内容，显示轮换选择框
		}
	}
	//如果没有提取缩略图，就不能选择 轮换图
	var imageval = $('#imageval').val();
	if (imageval.length < 1) {
		$('#lunhuantu').hide();
	} else {
		$('#lunhuantu').show();
	}
	$('#imageval').bind('input propertychange', function () {
		if ($(this).val().length) {
			$('#lunhuantu').show();
		} else {
			$('#lunhuantu').hide();
		}

	});
</script>